﻿layui.extend({
    dtree: '../../plugins/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
}).use(['dtree', 'layer', 'jquery'], function () {
    var dtree = layui.dtree, layer = layui.layer;
    var roleId = $("#roleid").val();
    dtree.render({
        elem: "#dataTree",
        url: "/Access/NodeStructure/" + roleId,
        dataFormat: "list",  //配置data的风格为list
        initLevel: "2",
        checkbar: true,
        checkbarType: "all",
        line: true,
        skin: "zdy",  // 自定义风格
        scroll: "#toolbarDiv", // 绑定div元素
    });

    // 绑定节点点击事件
    dtree.on("chooseDone('dataTree')", function (obj) {
        $("#save").removeClass("layui-btn-disabled");
        var ids = "";
        for (var i = 0; i < obj.checkbarParams.length; i++) {
            if (i == obj.checkbarParams.length - 1) {
                ids += obj.checkbarParams[i].nodeId;
            } else {
                ids += obj.checkbarParams[i].nodeId + ",";
            }
        }
        $("#nodeIds").val(ids);
    });
});

/**
 * 表单提交
 * @@param posturl
 * @@param backurl
 */
function saveNode(posturl, backurl) {
    $.ajax({
        url: posturl,
        type: "post",
        data: $("form").serialize(),
        success: function (rs) {
            var iconMsg = 2;
            if (rs.status == 1) {
                iconMsg = 1;
            }
            layer.open({
                title: "提示",
                icon: iconMsg,
                content: rs.msg,
                yes: function () {
                    if (rs.status == 1) {
                        window.location.href = backurl;
                    }
                }
            });
        },
        error: function (rs) {
            layer.msg("网络错误，保存失败！", { icon: 2 });
        }
    });
}